﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using StockStar.GatherData;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;

namespace Forex
{
    class Program
    {
        static void Main(string[] args)
        {
            upFuture();
            UpForex();

            upFutureSnap();
        }

        #region 采集期货信息
        public static void upFuture()
        {
            //string _url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=CONH0,HONH0,GLNJ0,SLNH0,CHCH0,SBCH0,WHCH0,CRCH0,SMCH0,SOCH0,CTNH0,LCPS0,LALS0,LZNS0,LTNS0,LNKS0,LLDS0";
            //string _url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=CONK0,HONJ0,GLNJ0,SLNN0,CHCK0,SBCK0,WHCK0,CRCK0,SMCK0,SOCK0,CTNK0,LCPS0,LALS0,LZNS0,LTNS0,LNKS0,LLDS0";
            string _url = GetQihuoUrl();
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            _Content = _Content.Replace("var js=", "");
            //var re = Newtonsoft.Json.JsonConvert.DeserializeObject(_Content);
            MatchCollection mcs = Regex.Matches(_Content, "\"(?<g1>[^\"]*)\"");
            StringBuilder sqlb = new StringBuilder();
            foreach (Match mc in mcs)
            {
                string value = mc.Value;
                string[] args = value.Split(new char[] { ',' });
                switch (args[0].Replace("\"", "").Substring(0, 3))
                {
                    case "CON":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='原油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "HON":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='燃料油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "GLN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='黄金'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SLN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='白银'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    //case "CHCH0":
                    //    sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}' where name='轻质原油'\r\n", args[5], args[18]);
                    //    break;
                    case "SBC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='大豆'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "WHC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='小麦'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "CRC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='玉米'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SMC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='豆粕'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SOC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='豆油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "CTN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='棉花'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LCP":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月铜'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LAL":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月铝'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LZN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月锌'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LTN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月锡'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LNK":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月镍'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LLD":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月铅'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    default:
                        break;
                }
            }

            _url = "http://bdcjhq.hexun.com/quote?s2=SUGAR.nme,undefined";
            _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            Match mc1 = Regex.Match(_Content, "la:\"(?<g1>[^\"]*)\"");
            Match mc2 = Regex.Match(_Content, "zdf:\"(?<g1>[^\"]*)\"");
            sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where name='白糖'\r\n", mc1.Groups["g1"].Value, mc2.Groups["g1"].Value);
            string sql = sqlb.ToString();
            #region 执行SQL
            if (!string.IsNullOrEmpty(sql))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                            trans.Commit();
                            Console.WriteLine("期货信息更新成功!");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Console.WriteLine("期货信息更新异常:" + ex.Message);
                            LogFile.WriteLog("期货信息更新异常:" + ex.Message);
                        }
                    }
                }
            }
            #endregion
        }

        public static void upFutureSnap()
        {
            //string _url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=CONH0,HONH0,GLNJ0,SLNH0,CHCH0,SBCH0,WHCH0,CRCH0,SMCH0,SOCH0,CTNH0,LCPS0,LALS0,LZNS0,LTNS0,LNKS0,LLDS0";
            //string _url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=CONK0,HONJ0,GLNJ0,SLNN0,CHCK0,SBCK0,WHCK0,CRCK0,SMCK0,SOCK0,CTNK0,LCPS0,LALS0,LZNS0,LTNS0,LNKS0,LLDS0";
            string _url = GetQihuoUrl();
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            _Content = _Content.Replace("var js=", "");
            //var re = Newtonsoft.Json.JsonConvert.DeserializeObject(_Content);
            MatchCollection mcs = Regex.Matches(_Content, "\"(?<g1>[^\"]*)\"");
            StringBuilder sqlb = new StringBuilder();
            foreach (Match mc in mcs)
            {
                string value = mc.Value;
                string[] args = value.Split(new char[] { ',' });
                switch (args[0].Replace("\"", "").Substring(0, 3))
                {
                    case "CON":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('原油','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "HON":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('燃料油','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='燃料油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "GLN":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('黄金','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='黄金'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SLN":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('白银','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='白银'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    //case "CHCH0":
                    //    sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}' where name='轻质原油'\r\n", args[5], args[18]);
                    //    break;
                    case "SBC":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('大豆','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='大豆'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "WHC":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('小麦','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='小麦'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "CRC":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('玉米','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='玉米'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SMC":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('豆粕','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='豆粕'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SOC":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('豆油','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='豆油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "CTN":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('棉花','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='棉花'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LCP":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('3月铜','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月铜'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LAL":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('3月铝','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月铝'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LZN":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('3月锌','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月锌'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LTN":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('3月锡','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月锡'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LNK":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('3月镍','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月镍'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LLD":
                        sqlb.AppendFormat("insert into ZS_ContentData_snap (name,newprice,ZDF,insertdate) values ('3月铅','{0}','{1}','{2}')\r\n", args[5], args[18], DateTime.Now.ToString());
                        //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdate='{2}' where name='3月铅'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    default:
                        break;
                }
            }

            //_url = "http://bdcjhq.hexun.com/quote?s2=SUGAR.nme,undefined";
            //_Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            //Match mc1 = Regex.Match(_Content, "la:\"(?<g1>[^\"]*)\"");
            //Match mc2 = Regex.Match(_Content, "zdf:\"(?<g1>[^\"]*)\"");
            //sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where name='白糖'\r\n", mc1.Groups["g1"].Value, mc2.Groups["g1"].Value);
            string sql = sqlb.ToString();
            #region 执行SQL
            if (!string.IsNullOrEmpty(sql))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                            trans.Commit();
                            Console.WriteLine("期货信息更新成功!");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Console.WriteLine("期货信息更新异常:" + ex.Message);
                            LogFile.WriteLog("期货信息更新异常:" + ex.Message);
                        }
                    }
                }
            }
            #endregion
        }
        #endregion

        #region 采集外汇数据
        private static void UpForex()
        {
            string _url = "http://qt.gtimg.cn/&r=0.22351773461386642q=EUR,JPY,AUD,USDCNY";
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            MatchCollection mcs = Regex.Matches(_Content, "\"(?<g1>[^\"]*)\"");
            StringBuilder sqlb = new StringBuilder();
            int i = 0;
            foreach (Match mc in mcs)
            {
                i++;
                string[] args = mc.Value.Replace("\"", "").Split(new char[] { ',' });
                float price = float.Parse(args[1]);
                float preclose = float.Parse(args[5]);
                float zdf = ((price - preclose) / price) * 100;
                switch (i)
                {
                    case 1:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=22", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    case 2:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=20", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    case 3:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=23", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    case 4:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=21", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    default:
                        break;
                }
            }

            string sql = sqlb.ToString();
            #region 执行SQL
            if (!string.IsNullOrEmpty(sql))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                            trans.Commit();
                            Console.WriteLine("外汇信息更新成功!");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Console.WriteLine("外汇信息更新异常:" + ex.Message);
                            LogFile.WriteLog("外汇信息更新异常:" + ex.Message);
                        }
                    }
                }
            }
            #endregion
        }
        #endregion

        /// <summary>
        /// 获取东财外汇数据链接
        /// </summary>
        /// <returns>返回链接</returns>
        public static string GetQihuoUrl()
        {
            string _url = "http://hqres.eastmoney.com/EMQuote_Qihuo/js/quote_qihuo.js";
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");

            MatchCollection mcs = Regex.Matches(_Content, "(?<g1>[A-Z]{4}0)\\|");

            StringBuilder argsb = new StringBuilder();

            foreach (Match mc in mcs)
            {
                argsb.AppendLine(mc.Groups["g1"].Value);
            }

            string args = argsb.ToString().Replace("\r\n", ",");
            string url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=" + args.Substring(0, args.Length - 1);
            return url;
        }
    }
}
